package com.lizk.imgbed.utils;

import com.lizk.imgbed.common.enums.NotifyType;
import com.sun.glass.ui.Screen;
import com.sun.javafx.stage.StageHelper;
import com.sun.javafx.tk.Toolkit;
import com.sun.javafx.util.Logging;
import javafx.animation.FadeTransition;
import javafx.animation.PathTransition;
import javafx.concurrent.ScheduledService;
import javafx.concurrent.Task;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.shape.*;
import javafx.stage.Popup;
import javafx.util.Duration;
import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author lizhikui
 * @date 2019/12/15 17:00
 */
public class UIUtils {
    private static final Logger logger = LoggerFactory.getLogger(UIUtils.class);

    public static Pair<Integer,Integer> getScreenVisibleXY(){
        Screen object = (Screen) Toolkit.getToolkit().getPrimaryScreen();
        return new Pair<>(object.getVisibleWidth(),object.getVisibleHeight());
    }
    public static Label createLabel(String msg,String style){
        Label label = new Label(msg);
        if(style != null && !style.equals("")){
            label.setStyle(style);
        }
        return label;
    }

    public static void notify(String msg, NotifyType notifyType){
        Pair<Integer, Integer> screenVisibleXY = getScreenVisibleXY();

        Integer screenWidth = screenVisibleXY.getKey();
        Integer screenHeight = screenVisibleXY.getValue();

        Integer popupWidth = screenWidth/5;
        Integer popupHeight = screenHeight/9;

        String labelStyle = null;
        if (notifyType == NotifyType.NOTIFY_ERROR){
            labelStyle = String.format("-fx-background-color: #f8d7da;-fx-text-fill: #721c76;-fx-wrap-text: true;-fx-padding: 20px;-fx-font-size: 16;-fx-pref-width: %d;-fx-pref-height: %d",popupWidth,popupHeight);
        }else if(notifyType == NotifyType.NOTIFY_SUCCESS){
            labelStyle = String.format("-fx-background-color: #d4edda;-fx-text-fill: #155724;-fx-wrap-text: true;-fx-padding: 20px;-fx-font-size: 16;-fx-pref-width: %d;-fx-pref-height: %d",popupWidth,popupHeight);
        }
        Label label = createLabel(msg, labelStyle);

        Popup popup = new Popup();
        popup.setX(screenWidth - popupWidth);
        popup.setY(screenHeight - popupHeight*1.5);
        popup.getContent().add(label);

        StackPane sp = new StackPane();
        PathTransition pt = new PathTransition();
        Path path = new Path();

        MoveTo moveTo = new MoveTo(screenWidth + popupWidth/2, 0);
        moveTo.setAbsolute(true);
        path.getElements().add(moveTo);

        LineTo lt = new LineTo(screenWidth - popupWidth + popupWidth/2, 0);
        lt.setAbsolute(true);
        path.getElements().add(lt);


        pt.setPath(path);
        pt.setDuration(Duration.millis(500));
        pt.setNode(label);
        pt.play();

        popup.show(StageHelper.getStages().get(0));


        delayRun(Duration.seconds(2), event -> {
            FadeTransition ftt  = new FadeTransition(Duration.seconds(1),label);
            ftt.setFromValue(1);
            ftt.setToValue(0.1);
            ftt.play();
            ftt.setOnFinished(event1 -> popup.hide());
        });
    }

    public static void delayRun(Duration duration, EventHandler<WorkerStateEvent> eventHandler){
        ScheduledService<Object> service = new ScheduledService<Object>(){
            @Override
            protected Task<Object> createTask() {
                return new Task<Object>() {
                    @Override
                    protected Object call() throws Exception {
                        return null;
                    }
                };
            }
        };

        service.setDelay(duration);
        service.setOnSucceeded(event -> {
            logger.info("延迟任务开始执行");
            eventHandler.handle((WorkerStateEvent)event);
            service.cancel();
            logger.info("延迟任务执行结束");
        });
        service.setOnFailed(event -> {
            logger.info("执行失败");
            service.cancel();
            logger.info("取消任务");
        });
        service.start();
    }




}
